// store/index.js - 简化版本
import { createStore } from 'vuex'

const store = createStore({
  state: {
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}'),
    token: localStorage.getItem('token') || '',
  },

  getters: {
    userInfo: (state) => state.userInfo,
    token: (state) => state.token,
    isLoggedIn: (state) => !!state.token,
  },

  mutations: {
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },

    SET_TOKEN(state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },

    CLEAR_USER(state) {
      state.userInfo = {}
      state.token = ''
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
    },
  },

  actions: {
    setUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
    },

    setToken({ commit }, token) {
      commit('SET_TOKEN', token)
    },

    logout({ commit }) {
      commit('CLEAR_USER')
    },
    ogout({ commit }) {
      // 清除用户信息
      commit('SET_USER', null)
      // 清除token
      commit('SET_TOKEN', '')
      // 清除其他需要清除的状态...
    },
  },
})

export default store
